﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using wrsm.battlenet.entity;

namespace wrsm.client {
    
    /// <summary>
    /// Tracks Realm Status Changes, and helps ensure status change notifications are shown,
    /// while not being quite so crazy
    /// </summary>
    internal class RealmStatusChangeNotificationManager {

        #region [Settings and Constants]

        /// <summary>
        /// Default Timer Interval (3 seconds)
        /// </summary>
        private readonly double DEFAULT_TIMER_INTERVAL = 3000;

        #endregion

        #region [Local Attributes]

        /// <summary>
        /// A collection of realm status objects to notify about changes
        /// </summary>
        private LinkedList<RealmStatus> _notifyList;

        /// <summary>
        /// Notification Timer
        /// </summary>
        private System.Timers.Timer _notifyTimer;

        #endregion

        #region [Notification Events]

        /// <summary>
        /// Event indicating a notification of a realm status change
        /// </summary>
        public event RealmStatusHandler RealmStatusChangedNotification;

        #endregion

        #region [Construction]

        /// <summary>
        /// Initialize Realm Status Change Notification manager
        /// </summary>
        public RealmStatusChangeNotificationManager() {
            WireupEvents();
            _notifyList = new LinkedList<RealmStatus>();
            _notifyTimer = CreateTimer();
            //Will likely want to be able to control this from the UI, 
            //so might need to add some functionality to halt collecting 
            //realm status changes and showing them if the user does not wish to see them...
            _notifyTimer.Start();
        }

        #endregion

        #region [Public Methods]

        /// <summary>
        /// Add a realm status to notification list
        /// </summary>
        /// <param name="status">Realm Status Change to notify for</param>
        public void AddStatus(RealmStatus status) {
            _notifyList.AddLast(status);
        }

        #endregion

        #region [The Guts]

        /// <summary>
        /// Create our Notification timer
        /// </summary>
        /// <returns>new notification timer</returns>
        private System.Timers.Timer CreateTimer() {
            System.Timers.Timer timer;
            double interval;
            //Attempt to create a timer with configured interval
            if (double.TryParse(ConfigurationManager.AppSettings["wrsm.client.notify.interval"], out interval)) {
                timer = new System.Timers.Timer(interval);
            }
            //Otherwise, use the default interval value
            timer = new System.Timers.Timer(DEFAULT_TIMER_INTERVAL);
            timer.Elapsed += new System.Timers.ElapsedEventHandler(OnNotificationTimerElapsed);

            return timer;
        }

        /// <summary>
        /// Get's the next realm status to notify about
        /// </summary>
        /// <returns>returns the next realm status, null if there is nothing to show</returns>
        private RealmStatus GetNextStatus() {
            RealmStatus status;
            //Retrn nothing, if there is nothing to return
            if (_notifyList.Count == 0) {
                return null;
            }
            //Grab the first status, and remove it from the list
            status = _notifyList.First.Value;
            _notifyList.RemoveFirst();
            //return the status
            return status;
        }

        #endregion

        #region [Event Implementation]

        /// <summary>
        /// Simply Wireup Events
        /// </summary>
        private void WireupEvents() {
            RealmStatusChangedNotification += new RealmStatusHandler(OnRealmStatusChangedNotification);
        }

        /// <summary>
        /// Internal Handler for OnRealmStatusChangedNotification
        /// </summary>
        /// <param name="status">Realm Status that has changed</param>
        private void OnRealmStatusChangedNotification(RealmStatus status) {
        }

        /// <summary>
        /// Fires when Notification timer elapses
        /// </summary>
        private void OnNotificationTimerElapsed(object source, System.Timers.ElapsedEventArgs args) {
            //TODO: Determine if there is anything to notify about
            RealmStatus status = GetNextStatus();
            if (status == null) {
                return;
            } else {
                RealmStatusChangedNotification(status);
            }
        }

        #endregion

    }
}
